package com.xudow.app.ui.user;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.task.user.ProfileEditTask;
import com.xudow.app.util.DateUtil;
import com.xudow.app.util.Maps;
import com.xudow.app.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * 生日编辑Activity
 *
 * @author lukecheng
 */
public class BirthdayEditActivity extends BaseActivity {

    private EditText birthdayEditText;

    private ProfileEditTask profileEditTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_birthday_edit);

        final String birthday = getIntent().getStringExtra("birthday");

        birthdayEditText = (EditText) findViewById(R.id.birthday);
        birthdayEditText.setText(birthday);

        birthdayEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date date = new Date();
                if (!StringUtils.isEmpty(birthday)) {
                    date = DateUtil.strToDate(birthday, DateUtil.DATE_FORMAT_YYYY_MM_DD);
                }

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);

                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                new DatePickerDialog(BirthdayEditActivity.this, dateSetListener, year, month, day).show();
            }
        });
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 保存按钮点击事件
     *
     * @param view
     */
    public void onSave(View view) {
        String birthday = birthdayEditText.getText().toString().trim();
        if (StringUtils.isEmpty(birthday)) {
            birthdayEditText.setError(getString(R.string.birthday_required));
            return;
        }

        HashMap<String, String> params = Maps.newHashMap();
        params.put("birthday", birthday);

        showLodingDialog(getString(R.string.processing));

        profileEditTask = new ProfileEditTask(this, profileEditHandler);
        profileEditTask.execute(params);
    }

    /**
     * 更新处理Handler
     */
    private Handler profileEditHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (ProfileEditTask.SUCCESS == msg.what) {
                String successMsg = getString(R.string.update_success);
                Toast.makeText(BirthdayEditActivity.this, successMsg, Toast.LENGTH_LONG).show();

                finish();
            } else if (ProfileEditTask.ERROR_WITH_MESSAGE == msg.what) {
                String errMessage = msg.getData().getString("errMessage");
                Toast.makeText(BirthdayEditActivity.this, errMessage, Toast.LENGTH_LONG).show();
            } else if (ProfileEditTask.ERROR == msg.what) {
                String errMessage = getString(R.string.update_error);
                Toast.makeText(BirthdayEditActivity.this, errMessage, Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode && null != profileEditTask
                && !profileEditTask.isCancelled()) {
            profileEditTask.cancel(true);
            profileEditTask = null;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 生日选择
     */
    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String birthday = String.format("%d-%d-%d", year, monthOfYear + 1, dayOfMonth);
            birthdayEditText.setText(birthday);
        }
    };
}
